package com.bw.educate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.educate.common.ResponseResult;
import com.bw.educate.common.auth.UserInfo;
import com.bw.educate.common.utils.MD5Util;
import com.bw.educate.entity.EduUser;
import com.bw.educate.entity.SysUser;
import com.bw.educate.mapper.SysUserMapper;
import com.bw.educate.service.IEduUserService;
import com.bw.educate.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 在线教育-五组
 * @since 2020-08-12
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * @Description: 管理员登录
     * @Author: 王文佳
     * @Date: 2020/8/14 15:24
     * @param user:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @Override
    public ResponseResult login(EduUser user, HttpServletResponse response) {
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            String pwd =MD5Util.md5Encrypt32Lower(user.getLoginPwd());
            queryWrapper.eq("LOGIN_NAME",user.getLoginName());
            queryWrapper.eq("LOGIN_PWD", pwd);

            List<SysUser> list = userMapper.selectList(queryWrapper);
            if(list.size()==0 || list==null){//登录不正确
                result.setSuccess(false);
                result.setMessage("登陆失败，用户名或密码不正确！！！");
                return result;
            }

            SysUser sysUser = list.get(0);

            if(sysUser.getRoleId()!=0){
                result.setSuccess(false);
                result.setMessage("登陆失败，请用管理员账户进行登录！！！");
                return result;
            }

            //随机生成一个token，存储到浏览器的cookie中，同时token存储到redis当中【cookie+redis实现了登录】
            UserInfo userInfo = new UserInfo(sysUser.getId(),sysUser.getLoginName());
            String token = UUID.randomUUID().toString();//生成随机token
            Cookie cookie = new Cookie("token", token);
            cookie.setMaxAge(1800);//超时时间  设置最长时间--秒
            cookie.setPath("/");
            response.addCookie(cookie);

            //将用户信息存储到redis中
            redisTemplate.opsForValue().set(token,userInfo);
            redisTemplate.expire(token,30, TimeUnit.MINUTES);

            Map map = new HashMap();
            map.put("user",sysUser);
            map.put("token",token);

            result.setMessage("登陆成功！！！");
            result.setResult(map);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("登陆失败！！！"+e.toString());
            e.printStackTrace();
        }
        return result;
    }
}
